package com.sujeet.learning.threads;

import java.util.concurrent.*;

/*
 * One beautiful babe
 */
enum Billo {
	KAREENA, KATRINA, PRIYANKA, DEEPIKA
}

/*
 * Babe's khaduus dad
 */
class BilloKaBaap {

	private Billo billo = Billo.DEEPIKA;
	private boolean phasaHai = false;

	BilloKaBaap(Billo b) {
		billo = b;
	}

	BilloKaBaap() {
	}

	public void phasao() {
		phasaHai = true;
	}

	public void chodo() {
		phasaHai = false;
	}

	public boolean phasaHai() {
		return phasaHai;
	}

	public synchronized Billo getBillo() {
		if (phasaHai) {
			System.out.println(Thread.currentThread().getName()
					+ " baap phasa liya hai");
			chodo();			
			notifyAll();
			 
		} else {
			System.out.println(Thread.currentThread().getName() + " failed to phasao baap");
			try {
				wait(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return billo;
	}

}

/*
 * Ashiq - Lover
 */
class Deewana implements Runnable {

	String naam;
	BilloKaBaap sasura;

	Deewana(String naam, BilloKaBaap billoKaBaap) {
		this.naam = naam;
		sasura = billoKaBaap;
	}

	public void run() {
		int i=0;
		while (true) {
			++i;
			Thread.currentThread().setName(naam + ": run no " + i);
			System.out.println(Thread.currentThread().getName()
					+ " :Trying to get Billo");
			if (!sasura.phasaHai()) {
				sasura.phasao();
				Billo myBillo = sasura.getBillo();
				System.out.println(Thread.currentThread().getName()
						+ " :-) got Billo: " + myBillo
						+ "-ligting my bidi with her heart");
				try {
					TimeUnit.MILLISECONDS.sleep(2000);
					System.out.println(Thread.currentThread().getName()
							+ " date with " + myBillo
							+ " got over in 2000 millisecs");
				} catch (InterruptedException ie) {
					ie.printStackTrace();
				}			

			} else {
				sasura.chodo();
			}
		}
	}
}

public class CompetingThreads {

	static BilloKaBaap baap = new BilloKaBaap(Billo.KATRINA);

	public static void main(String[] args) {
		ExecutorService es = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 10; i++) {
			es.execute(new Deewana(("Deewana no " + i), baap));
		}
	}

}
